﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Economo.Services.BusinessLogic;
using Economo.Services.BusinessEntities;

namespace DataLayerTestProject
{
    [TestClass]
    public class TestBankAccounts
    {

        [TestMethod]
        public void TC_Accounts_01_GetBankAccounts()
        {
            BankAccountsBusinessLogic bl = new BankAccountsBusinessLogic();
            List<BankAccount> accounts = bl.GetBankAccounts();
            Assert.AreEqual(accounts.Count, 4);
            List<BankAccount> citi = bl.GetBankAccounts(2);
            Assert.AreEqual(citi.Count, 2);
            BankAccount santanderPesos = bl.GetAccountById(1, 3);
            Assert.IsNotNull(santanderPesos);
            BankAccount citiPesos = bl.GetAccountById(1);
            Assert.IsNotNull(citiPesos);
            List<BankAccount> bankNada = bl.GetBankAccounts(100);
            Assert.AreEqual(bankNada.Count, 0);
            BankAccount nada = bl.GetAccountById(100);
            Assert.IsNull(nada);
        }
        [TestMethod]
        public void TC_Accounts_02_InsertAndDeleteBankAccounts()
        {
            BankAccountsBusinessLogic bl = new BankAccountsBusinessLogic();
            Int32 preInsert = bl.GetBankAccounts().Count;
            BankAccount newAccount = new BankAccount();
            newAccount.BankId = 1;
            newAccount.Description = "borrar";
            newAccount.CurrencyId = 0;
            Int16 newId = bl.InsertAccount(newAccount);
            Int32 postInsert = bl.GetBankAccounts().Count;
            Assert.AreEqual(preInsert + 1, postInsert);
            bl.DeleteAccount(newAccount.BankId,newId);
            Int32 postDelete = bl.GetBankAccounts().Count;
            Assert.AreEqual(postInsert - 1, postDelete);
            try
            {
                bl.DeleteAccount(1,150);
                Assert.Fail("Wrong Delete didn`t throw an error message");
            }
            catch
            {
                //This delete must fail
            }
        }

        [TestMethod]
        public void TC_Accounts_03_UpdateBankAccounts()
        {
            BankAccountsBusinessLogic bl = new BankAccountsBusinessLogic();
            BankAccount oldAccount = bl.GetAccountById(3);            
            BankAccount toUpdate = new BankAccount();
            toUpdate.Id = 3;
            toUpdate.BankId = oldAccount.BankId;
            toUpdate.Description = "Testing Description";
            bl.UpdateAccount(toUpdate);
            BankAccount updatedAccount = bl.GetAccountById(3);
            Assert.AreEqual(updatedAccount.Description, "Testing Description");            
            bl.UpdateAccount(oldAccount);
        }
    }    
}
